package com.xm.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import com.xm.uti.DBManager;
import com.xm.entity.Banners;
import com.xm.entity.producttype;
import com.xm.uti.QueryDataResult;


/**
 * 产品分类列表持久层的实现（全查）
 * @author kh
 *
 */
public class ProtypeDaoImpl {
	public ArrayList<producttype> getAllProtype() {
		String sql = "select * from producttype";
		System.out.println("执行的sql语句是" + sql);
		QueryDataResult execQuery = DBManager.executeQuery(sql);
		ArrayList<producttype> list = new ArrayList<producttype>();
		if (execQuery != null) {
			ResultSet rs = execQuery.rs;
			try {
				while (rs.next()) {
					producttype e = new producttype(rs.getInt(1),rs.getString("Typename"));
					list.add(e);
				}
			} catch (SQLException e) {
				// TODO: handle exception
				e.printStackTrace();
			} finally {
				execQuery.close();
			}
		}
		System.out.println(list);
		return list;
	}
	
	/**
	   * 重名验证
	   * 
	   * @param emp
	   * @return
	   */
	  public producttype insertPtyName(String typename) {
	    String sql = "select * from producttype where typename = '" + typename + "'";
	    System.out.println("执行的sql语句是" + sql);
	    QueryDataResult execQuery = DBManager.executeQuery(sql);
	    if (execQuery != null) {
	      ResultSet rs = execQuery.rs;
	      try {
	        while (rs.next()) {
	          producttype e = new producttype(rs.getInt(1), rs.getString("Typename"));
	          return e;
	        }
	      } catch (SQLException e) {
	        // TODO: handle exception
	        e.printStackTrace();
	      } finally {
	        execQuery.close();
	      }
	    }
	    return null;
	  }
	
	
	/**
	 * 增
	 * 
	 * @param emp
	 * @return
	 */
	public boolean insertPty(producttype proty) {
		String sql = "insert into producttype (typename) values('" + proty.getTypename() + "')";
		System.out.println("执行的sql语句是" + sql);
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 删除
	 * 
	 * @param emp
	 * @return
	 */
	public boolean delPty(int id) {
		String sql = "delete from producttype where id = " + id;
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	/**
	 * 修改
	 * 
	 * @param emp
	 * @return
	 */
	public boolean updatePty(producttype Pty) {
		System.out.println("产品系列的数据是"+Pty);
		String sql = "update producttype set typename='" + Pty.getTypename() + "' where id =" + Pty.getId();
		int i = DBManager.executeUpdate(sql);
		if (i > 0) {
			return true;
		}
		return false;
	}

	



	
}